﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CSharp.Libs.WPF;
using CSharp.Libs.ExceptionHandling;
using System.Collections.ObjectModel;
using System.Collections;
using System.Windows.Input;

namespace CSharp.Libs.WPF.UserControls
{
    class AutoKocUsersViewModel : ViewModelBase
    {
        #region declarations
        private ExceptionCollector _EC = null;
        private AutoKocUsersModel _AUM = null;

        private RelayCommand _rcGetUsers = null;
        private RelayCommand _rcDeleteUser = null;
        private RelayCommand _rcInsertUser = null;

        private ObservableCollection<AutoKocUserItem> _ocUsers = new ObservableCollection<AutoKocUserItem>();
        private AutoKocUserItem _akuiSelectedUser = null;
        private Int64 _intFbUid = -1;
        private String _strName = "";

        public enum VariableTypes
        {
            String = 1,
            Int32,
            Int64,
            Double,
            Boolean
        }
        #endregion



        #region constructor / terminator
        public AutoKocUsersViewModel()
        {
            _EC = new ExceptionCollector();
            _AUM = new AutoKocUsersModel(_EC);

            _GetUsers();
        }

        ~AutoKocUsersViewModel()
        {
        }
        #endregion



        #region private functions
        /// <summary>_GetUsers</summary>
        /// <history>28.07.2011</history>
        private void _GetUsers()
        {
            try
            {
                _ocUsers.Clear();

                ArrayList aryUsers = _AUM.GetTable();

                foreach (Hashtable htEntry in aryUsers)
                {
                    AutoKocUserItem AKUI = new AutoKocUserItem();

                    AKUI.ID = _GetHashtableValue(htEntry, "ID", VariableTypes.Int32);
                    AKUI.FbUid = _GetHashtableValue(htEntry, "FbUid", VariableTypes.Int64);
                    AKUI.Name = _GetHashtableValue(htEntry, "Name", VariableTypes.String);

                    _ocUsers.Add(AKUI);
                }

                OnPropertyChanged("Users");
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        private void _DeleteUser()
        {
            try
            {
                if (CanDeleteUser)
                {
                    _AUM.DeleteFbUid(_intFbUid);
                    _GetUsers();
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        private void _InsertUser()
        {
            try
            {
                if (CanInsertUser)
                {
                    _AUM.InsertFbUid(_intFbUid, _strName);
                    _GetUsers();
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }
        }

        #region helping functions
        /// <summary>IsNumeric</summary>
        /// <param name="Expression"></param>
        /// <returns></returns>
        /// <history>28.07.2011 taken from http://blog.schelian.de/2007/07/isnumeric-c-oder-csharp/ </history>
        public Boolean IsNumeric(object Expression)
        {
            Boolean isNum;
            Double retNum;

            isNum = Double.TryParse(Convert.ToString(Expression), System.Globalization.NumberStyles.Any, System.Globalization.NumberFormatInfo.InvariantInfo, out retNum);

            return isNum;
        }

        /// <summary>_GetHashtableValue</summary>
        /// <param name="HT"></param>
        /// <param name="Key"></param>
        /// <param name="ReturnType"></param>
        /// <returns></returns>
        /// <history>15.05.2011</history>
        private dynamic _GetHashtableValue(
            Hashtable HT,
            String Key,
            VariableTypes ReturnType)
        {
            try
            {
                return _GetObjectValue((Object)HT[Key], ReturnType);
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return null;
        }

        /// <summary>_GetObjectValue</summary>
        /// <param name="objValue"></param>
        /// <param name="ReturnType"></param>
        /// <returns></returns>
        /// <history>09.06.2011</history>
        private dynamic _GetObjectValue(
            Object objValue,
            VariableTypes ReturnType)
        {
            try
            {
                switch (ReturnType)
                {
                    case VariableTypes.String:
                        if (objValue == null)
                        {
                            return "";
                        }
                        return objValue.ToString();

                    case VariableTypes.Double:
                        if (objValue == null)
                        {
                            return (Double)0;
                        }
                        Double dblRetVal = 0;
                        Double.TryParse(objValue.ToString(), out dblRetVal);
                        return dblRetVal;

                    case VariableTypes.Int32:
                        if (objValue == null)
                        {
                            return (Int32)0;
                        }
                        Int32 int32RetVal = 0;
                        Int32.TryParse(objValue.ToString(), out int32RetVal);
                        return int32RetVal;

                    case VariableTypes.Int64:
                        if (objValue == null)
                        {
                            return (Int64)0;
                        }
                        Int64 int64RetVal = 0;
                        Int64.TryParse(objValue.ToString(), out int64RetVal);
                        return int64RetVal;

                    case VariableTypes.Boolean:
                        if (objValue == null)
                        {
                            return false;
                        }
                        Boolean fRetVal = false;
                        Boolean.TryParse(objValue.ToString(), out fRetVal);
                        return fRetVal;
                }
            }
            catch (Exception ex)
            {
                _EC.AddException(ex, this.ToString(), _EC.Tools.GetCurrentMethodName());
            }

            return null;
        }
        #endregion
        #endregion



        #region interface
        #region properties
        public ObservableCollection<AutoKocUserItem> Users
        {
            get
            {
                return _ocUsers;
            }
        }

        public AutoKocUserItem SelectedUser
        {
            get
            {
                return _akuiSelectedUser;
            }
            set
            {
                _akuiSelectedUser = value;

                if (_akuiSelectedUser != null)
                {
                    _intFbUid = _akuiSelectedUser.FbUid;
                    _strName = _akuiSelectedUser.Name;
                }
                else
                {
                    _intFbUid = -1;
                    _strName = "";
                }

                OnPropertyChanged("FbUid");
                OnPropertyChanged("Name");
            }
        }

        public String Name
        {
            get
            {
                return _strName;
            }
            set
            {
                _strName = value;
            }
        }

        public Int64 FbUid
        {
            get
            {
                return _intFbUid;
            }
            set
            {
                _intFbUid = value;
            }
        }
        

        public ICommand GetUsers
        {
            get
            {
                if (_rcGetUsers == null)
                {
                    _rcGetUsers = new RelayCommand(
                        param => this._GetUsers(),
                        param => this.CanGetUsers);
                }
                return _rcGetUsers;
            }
        }
        public Boolean CanGetUsers
        {
            get
            {
                return true;
            }
        }

        public ICommand DeleteUser
        {
            get
            {
                if (_rcDeleteUser == null)
                {
                    _rcDeleteUser = new RelayCommand(
                        param => this._DeleteUser(),
                        param => this.CanDeleteUser);
                }
                return _rcDeleteUser;
            }
        }
        public Boolean CanDeleteUser
        {
            get
            {
                return _intFbUid >= 0;
            }
        }
        
        public ICommand InsertUser
        {
            get
            {
                if (_rcInsertUser == null)
                {
                    _rcInsertUser = new RelayCommand(
                        param => this._InsertUser(),
                        param => this.CanInsertUser);
                }
                return _rcInsertUser;
            }
        }
        public Boolean CanInsertUser
        {
            get
            {
                return _intFbUid >= 0;
            }
        }
        #endregion

        #region methods
        #endregion
        #endregion
    }
}
